package com.ruoyi.dm.component;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import com.ruoyi.common.constant.CollectFileTypeConstants;
import com.ruoyi.common.utils.biz.BizModule;
import com.ruoyi.common.utils.biz.PathUtil;
import com.ruoyi.dm.domain.po.GroundCollectRecordPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 地基 雷达bath 大气监测数据
 */
@Component
@Slf4j
public class RadarBathDataStrategy implements DataStrategy<GroundCollectRecordPO> {

    @Override
    public GroundCollectRecordPO makeupPo(GroundCollectRecordPO po) {

        String fileName = po.getFileName();
        //bath-skiyment-meteor-radar_esrange_20160103_daily-winds.hwd
        String[] split = fileName.split("_");
        LocalDate yyyyMMhh = LocalDate.parse(split[2], DateTimeFormatter.ofPattern("yyyyMMdd"));
        po.setFileDate(yyyyMMhh);

        //中间路径
        int year = po.getFileDate().getYear();
        String filePath = po.getFilePath();
        filePath += "/"+po.getStation()+"/" + year;
        po.setFilePath(filePath);
        return po;
    }

    @Override
    public String pythonCmd(String station, String dataType, String filePath, String method, Integer year) {
        String fileType = CollectFileTypeConstants.TYPE_RADAR;
        //传文件夹  到 /WIND ,算法自动检索
        String outPath = PathUtil.outputModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("result") ;
        FileUtil.mkdir(outPath);
        String logPath = PathUtil.tmpModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("result");
        FileUtil.mkdir(logPath);
        String script = PathUtil.codeModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("bathScript");
        String[] cmdLines = new String[10];
        cmdLines[0] = "python";
        cmdLines[1] = script;
        cmdLines[2] = "--filepath";//原始文件
        cmdLines[3] = filePath;
        cmdLines[4] = "--outfile";//产品路径
        cmdLines[5] = outPath;
        cmdLines[6] = "--outlog";//日志路径
        cmdLines[7] = outPath;
        cmdLines[8] = "--times";//解析年份
        cmdLines[9] = year+"";
        String commond = ArrayUtil.join(cmdLines, " ");
        log.info("拼接脚本:{}", commond);
        return commond;
    }
}
